package org.example;

import java.util.ArrayList;
import java.util.List;

public class Test1 {
    //leetcode 螺旋矩阵
    // https://leetcode.cn/problems/spiral-matrix/description/?envType=study-plan-v2&envId=top-interview-150
    public List<Integer> spiralOrder(int[][] matrix) {
        int n1 = matrix.length, n2 = matrix[0].length;
        int row = 0, col = -1;
        List<Integer> ret = new ArrayList<>();
        int leftBorder = 0, rightBorder = n2-1, topBorder = 1, bottomBorder = n1-1;
        while (rightBorder >= leftBorder || topBorder <= bottomBorder) {
            while (col < rightBorder) {
                ret.add(matrix[row][++col]);
            }
            rightBorder--;
            if (topBorder > bottomBorder) break;
            while (row < bottomBorder) {
                ret.add(matrix[++row][col]);
            }
            bottomBorder--;
            if (leftBorder > rightBorder) break;
            while (col > leftBorder) {
                ret.add(matrix[row][--col]);
            }
            leftBorder++;
            if (topBorder > bottomBorder) break;
            while (row > topBorder) {
                ret.add(matrix[--row][col]);
            }
            topBorder++;
            if (leftBorder > rightBorder) break;
        }
        return ret;
    }

    public static void main(String[] args) {
        int[][] matrix = new int[3][1];
        matrix[0][0] = 7;
        matrix[1][0] = 9;
        matrix[2][0] = 6;
        Test1 test1 = new Test1();
        test1.spiralOrder(matrix);
        System.out.println(777);
    }
}
